﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Core.Configuration;
using MvcEngine.Core.Helpers;
using MvcEngine.Core;
using System.Data.Linq;
using System.Reflection;
using MvcEngine.Core.Exceptions;
using System.Data.Linq.Mapping;
using System.IO;

namespace MvcEngine.LinqToSqlProvider
{
    public class CoreDataContextService : IDataContextService
    {
        protected virtual Stream GetMappingFromAssembly(Assembly assembly)
        {
            string[] embeddedFiles = assembly.GetManifestResourceNames();
            var mappingFile = embeddedFiles.FirstOrDefault(f => f.EndsWith(".mapping.xml"));
            if (mappingFile == null)
                throw new BusinessException("Mapping file was not found");
            return assembly.GetManifestResourceStream(mappingFile);
        }

        public virtual Stream GetMappingFile()
        {
            return GetMappingFromAssembly(Assembly.GetExecutingAssembly());
        }
    }
}
